package com.sky.context;

import com.sky.entity.Employee;
import com.sky.entity.User;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

public class BaseContext {

    public static ThreadLocal<Long> threadLocal = new ThreadLocal<>();

    public static void setCurrentId(Long id) {
        threadLocal.set(id);
    }

    public static Long getCurrentId() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Object principal = authentication.getPrincipal();
        if (principal instanceof Employee) {
            Employee employee = (Employee) authentication.getPrincipal();
            return employee.getId();
        }
        if (principal instanceof User) {
            User user = (User) authentication.getPrincipal();
            return user.getId();
        }
        return 99999999L;
    }

    public static void removeCurrentId() {
        threadLocal.remove();
    }

}
